﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TjZheng.Framework;
using TjZheng.WeiXin.DataEntity;

namespace TjZheng.WeiXin.SDK.Pay
{
    public class PayModeTwo : NativePay
    {
        public UnifiedOrderProcessHandle UnifiedOrderProcessHandle { get; private set; }
        public PayModeTwo(string appId)
            : base(appId)
        {
            this.UnifiedOrderProcessHandle = new UnifiedOrderProcessHandle();
        }
        public PayModeTwo(int merchantId)
            : base(merchantId)
        {
            this.UnifiedOrderProcessHandle = new UnifiedOrderProcessHandle();
        }
        public PayModeTwo(MerchantInfo objMerchantInfo)
            : base(objMerchantInfo)
        {
            this.UnifiedOrderProcessHandle = new UnifiedOrderProcessHandle();
        }
        public void SetUnifiedOrderProcessHandle(UnifiedOrderProcessHandle objUnifiedOrderProcessHandle)
        {
            if (objUnifiedOrderProcessHandle == null)
            {
                throw new ArgumentNullException("NativePayModelTwo.UnifiedOrderProcessHandle");
            }
            this.UnifiedOrderProcessHandle = objUnifiedOrderProcessHandle;
        }
        public override InvokeResult CreatePayUrl(WxPayPackage objWxPayPackage)
        {
            if (objWxPayPackage == null)
            {
                throw new ArgumentNullException("NativePayModelTwo.WxPayPackage");
            }
            UnifiedOrderProcessContext objUnifiedOrderProcessContext = new UnifiedOrderProcessContext();
            objUnifiedOrderProcessContext.SetMerchantInfo(this.MerchantInfo);
            objUnifiedOrderProcessContext.SetIntputWxPayPackage(objWxPayPackage);

            InvokeResult objInvokeResult = this.UnifiedOrderProcessHandle.ExecuteProcess(objUnifiedOrderProcessContext);
            if (objInvokeResult.ResultCode != "Success")
            {
                return objInvokeResult;
            }
            WxPayPackage objOutputWxPayPackage = objUnifiedOrderProcessContext.OutputWxPayPackage;
            string code_url = objOutputWxPayPackage.GetValue("code_url") as string;
            if (code_url.IsNull())
            {
                objInvokeResult.Data = objOutputWxPayPackage;
                return objInvokeResult;
            }
            objInvokeResult.Data = code_url;
            return objInvokeResult;
        }
    }
}
